// pages/calc/index.js
const App = getApp()
let sys = wx.getSystemInfoSync();
let systype = 1;
if (sys.system.indexOf('iOS') > -1 || sys.model.indexOf('iPhone') > -1) {
    systype = 2;
}
Page({
    /**
     * 页面的初始数据
     */
    data: {
        loading: true,
        vip2: '--',
        homePostParam: '',
        awayPostParam: '',
        commet: '',
        scoreArr: [],
        totalScore: [],
        topScore: [],
        Direction: {},
        spf: '',
        showDialog: false,
        showDialog2: false,
        tipsText: '',
        showMethodText: true,
        inputType: systype == 1 ? 'text' : 'number'
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: async function(options) {
        let isLogin = wx.getStorageSync('token');
        if (!isLogin) {
            // wx.reLaunch({
            //     url: '/pages/login/index',
            // })
        } else {
            //初始化输入框类型

            console.log(sys)
            //this.usertimes()
        }
    },
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    async onPullDownRefresh() {
        let isLogin = wx.getStorageSync('token');
        if (!isLogin) {
            this.setData({
                showDialog2: true,
                tipsText: '用户未登录，无法刷新',
            });
            wx.stopPullDownRefresh();
            return
        }
        this.setData({
            loading: true
        });
        await this.usertimes();
        wx.stopPullDownRefresh();
    },
    //上拉到底
    async onReachBottom() {},
    async usertimes() {
        App.showLoading();
        let {
            data
        } = await App.$http("/api/front/userviptimes");
        this.setData({
            vip2: data.vip2,
            loading: false
        });
        wx.hideLoading();
    },
    showOffMethodText() {
        this.setData({
            showMethodText: !this.data.showMethodText
        });
    },
    onListDialog() {
        let {
            vip2
        } = this.data;
        if (vip2 === '--') {
            this.setData({
                showDialog2: true,
                tipsText: '请您到【我的】页面登录后再使用此功能',
            });
            return
        }
        if (vip2 <= 0) {
            this.setData({
                showDialog2: true,
                tipsText: '您的使用次数不足，无法计算',
            });
            return
        }
        this.setData({
            showDialog: true
        });
    },
    onCloseDialog() {
        this.setData({
            showDialog: false
        });
    },
    onCloseDialog2() {
        this.setData({
            showDialog2: false
        });
    },
    async calc() {
        let {
            homePostParam,
            awayPostParam,
            vip2
        } = this.data;
        /*  homePostParam = '1 2.9 1 3';
         awayPostParam = '0.9 4.3 0.2 5'; */
        let harr = homePostParam.trim().replace(/\s+/g, '&').split('&');
        let aarr = awayPostParam.trim().replace(/\s+/g, '&').split('&');
        if (harr.length !== 4 || aarr.length !== 4) {
            this.setData({
                showDialog2: true,
                tipsText: '输入的参数数量未全部为4，无法计算',
            });
            return
        }
        try {
            App.showLoading();
            let {
                data
            } = await App.$http("/api/front/calcsingle", {
                homePostParam,
                awayPostParam
            });
            this.setData({
                scoreArr: data.res.totalArr,
                totalScore: data.res['总进球数'],
                topScore: data.res['计算比分'],
                Direction: data.res['Direction'],
                spf: data.res['spf'],
                vip2: data.vip2
            });
            wx.hideLoading();
        } catch (error) {
            this.setData({
                showDialog2: true,
                tipsText: error,
            });
            wx.hideLoading();
        }

    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {
        return {
            title: '当期竞彩测算',
            path: '/pages/index/index',
            imageUrl: '../../images/logo_share.png'
        }
    },
});